﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECommerce.Library.Web;
using AutoGear.BLL;
using ECommerce.Library;

public partial class myaccount_address_add : SitePublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
        if (!Page.IsPostBack)
        {
            if (SessionManager.IsUserLoggedIn)
            {
                SetPageTitleAndBreadcramb();
                uccShippingState.DataBind();
                uccShippingCountry.DataBind();
            }
            else
            {
                Response.Redirect(string.IsNullOrEmpty(SupplierId) ? CustomSiteMap.Login : "~/partner/" + SupplierId + "/login.aspx");
            }
        }
    }

    private void SetPageTitleAndBreadcramb()
    {
        ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("cphMiddleContent");

        Literal litPageTitle = (Literal)ph.FindControl("litPageTitle");
        Literal litCurrentPage = (Literal)ph.FindControl("litCurrentPage");

        if (litPageTitle != null)
        {
            litCurrentPage.Text = litPageTitle.Text = "Add Shipping Address";

        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            AddressBook addressbook = new AddressBook();
            addressbook.UserId = SessionManager.LoggedUser.UserId;
            addressbook.UserFirstName = tbxShippingFirstName.Text.ToString();
            addressbook.UserLastName = tbxShippingLastName.Text.ToString();
            if (!string.IsNullOrEmpty(tbxShippingPhoneNumber.Text))
            {
                string userTelephoneNumber = tbxShippingPhoneNumber.Text.Trim();
                if (!RegExUtil.IsPhoneNumber(userTelephoneNumber))
                {
                    uccResponseMessage.SetFailMessage("Incorrect Personal Phone Number.");
                    uccResponseMessage.Visible = true;
                    return;
                }
                else
                {
                    addressbook.UserTelephone1 = userTelephoneNumber;
                }
            }
            addressbook.UserTelephone2 = string.Empty;
            //if (!string.IsNullOrEmpty(tbxShippingEmailAddress.Text))
            //{
            //    string loginEmailAddress = tbxShippingEmailAddress.Text.Trim();
            //    if (!RegExUtil.IsEmail(loginEmailAddress))
            //    {
            //        uccResponseMessage.SetFailMessage("Incorrect Login Email.");
            //        uccResponseMessage.Visible = true;
            //        return;
            //    }
            //    else
            //    {
            //        addressbook.UserEmailAddress = loginEmailAddress;
            //    }
            //}
            addressbook.UserEmailAddress = string.Empty;
            addressbook.UserAddressLine1 = tbxShippingAddressLine1.Text;
            addressbook.UserAddressLine2 = tbxShippingAddressLine2.Text;
            addressbook.CompanyName = tbxCompanyName.Text;
            addressbook.UserCity = tbxShippingCity.Text;
            addressbook.UserState = uccShippingState.SelectedItem.Text;
            addressbook.UserZip = tbxShippingZipCode.Text;
            addressbook.UserCountry = uccShippingCountry.SelectedItem.Text;
            addressbook.IsDefault = cbxIsDefault.Checked;
            addressbook.Create();
            if (addressbook.AddressBookId != null)
            {
                uccResponseMessage.SetSuccessMessage("New Address created");
            }
        }
        catch (Exception ex) 
        {
            uccResponseMessage.SetFailMessage("Failed to create address.");
            uccResponseMessage.Visible = true;
        }
    }
}